package cn.highset.modules.base.serviceimpl;

import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.dao.ExhibitorDao;
import cn.highset.modules.base.entity.Exhibitor;
import cn.highset.modules.base.entity.ExhibitorProductType;
import cn.highset.modules.base.service.ExhibitorService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 展商接口实现
 *
 * @author Victor
 */
@Slf4j
@Service
@Transactional
public class ExhibitorServiceImpl implements ExhibitorService {

    @Autowired
    private ExhibitorDao exhibitorDao;

    @Override
    public ExhibitorDao getRepository() {
        return exhibitorDao;
    }

    @Override
    public Page<Exhibitor> findByCondition(Exhibitor exhibitor, SearchVo searchVo, Pageable pageable) {

        return exhibitorDao.findAll(new Specification<Exhibitor>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Exhibitor> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");

                List<Predicate> list = new ArrayList<Predicate>();
                //模糊搜素
                if (StrUtil.isNotBlank(exhibitor.getCompanyName())) {
                    list.add(cb.like(root.get("companyName"), '%' + exhibitor.getCompanyName() + '%'));
                }
                //模糊搜素
                if (StrUtil.isNotBlank(exhibitor.getCompanyNameEn())) {
                    list.add(cb.like(root.get("companyNameEn"), '%' + exhibitor.getCompanyNameEn() + '%'));
                }
                if (StrUtil.isNotBlank(exhibitor.getStandNo())) {
                    list.add(cb.like(root.get("standNo"), '%' + exhibitor.getStandNo() + '%'));
                }

                //产品类别
//                if (ArrayUtil.isNotEmpty(exhibitor.getProductTypeIds())) {
//                    Subquery<String> sq = cq.subquery(String.class);
//                    Root<ExhibitorProductType> from = sq.from(ExhibitorProductType.class);
//                    List<Predicate> subList = new ArrayList<Predicate>();
//                    CriteriaBuilder.In<String> in = cb.in(from.get("productTypeId"));
//                    for (String productId : exhibitor.getProductTypeIds()) {
//                        in.value(productId);
//                    }
//                    subList.add(in);
//                    Predicate[] subArr = new Predicate[subList.size()];
//                    //完成子查询
//                    sq.select(from.get("exhibitorId")).where(subList.toArray(subArr));
//                    list.add(cb.in(root.get("id")).value(sq));
//                }
                if (StrUtil.isNotEmpty(exhibitor.getProductTypeId())) {
                    Subquery<String> sq = cq.subquery(String.class);
                    Root<ExhibitorProductType> from = sq.from(ExhibitorProductType.class);
                    List<Predicate> subList = new ArrayList<Predicate>();
                    subList.add( cb.equal(from.get("productTypeId"),exhibitor.getProductTypeId()));
                    Predicate[] subArr = new Predicate[subList.size()];
                    //完成子查询
                    sq.select(from.get("exhibitorId")).where(subList.toArray(subArr));
                    list.add(cb.in(root.get("id")).value(sq));
                }
                //创建时间
                if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

    @Override
    public Page<Exhibitor> findByConditionByApi(Exhibitor exhibitor, Pageable pageable) {

        return exhibitorDao.findAll(new Specification<Exhibitor>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Exhibitor> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {


                List<Predicate> list = new ArrayList<Predicate>();
                //模糊搜素 多条件
                if (StrUtil.isNotBlank(exhibitor.getKey())) {
                    list.add(cb.or(cb.like(root.get("companyName"), '%' + exhibitor.getKey() + '%'),
                            cb.like(root.get("companyNameEn"), '%' + exhibitor.getKey() + '%'),
                            cb.like(root.get("standNo"), '%' + exhibitor.getKey() + '%')));
                }
                //产品类别
//                if (ArrayUtil.isNotEmpty(exhibitor.getProductTypeIds())) {
//                    Subquery<String> sq = cq.subquery(String.class);
//                    Root<ExhibitorProductType> from = sq.from(ExhibitorProductType.class);
//                    List<Predicate> subList = new ArrayList<Predicate>();
//
//                    CriteriaBuilder.In<String> in = cb.in(from.get("productTypeId"));
//                    for (String productId : exhibitor.getProductTypeIds()) {
//                        in.value(productId);
//                    }
//                    subList.add(in);
//                    Predicate[] subArr = new Predicate[subList.size()];
//                    //完成子查询
//                    sq.select(from.get("exhibitorId")).where(subList.toArray(subArr));
//                    list.add(cb.in(root.get("id")).value(sq));
//                }
                if (StrUtil.isNotEmpty(exhibitor.getProductTypeId())) {
                    Subquery<String> sq = cq.subquery(String.class);
                    Root<ExhibitorProductType> from = sq.from(ExhibitorProductType.class);
                    List<Predicate> subList = new ArrayList<Predicate>();
                    subList.add( cb.equal(from.get("productTypeId"),exhibitor.getProductTypeId()));
                    Predicate[] subArr = new Predicate[subList.size()];
                    //完成子查询
                    sq.select(from.get("exhibitorId")).where(subList.toArray(subArr));
                    list.add(cb.in(root.get("id")).value(sq));
                }


                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }
}
